package com.zeng.controller;

import com.zeng.entity.Kinds;
import com.zeng.entity.Result;
import com.zeng.service.KindsService;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Kinds)表控制层
 *
 * @author zengfanchao
 * @since 2021-03-17 19:17:04
 */
@RestController
@RequestMapping("kinds")
public class KindsController {
    /**
     * 服务对象
     */
    @Resource
    private KindsService kindsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Kinds selectOne(Integer id) {
        return this.kindsService.queryById(id);
    }

    @GetMapping("selectAll")
    public List<Kinds> selectAll() {
        List<Kinds> list = this.kindsService.selectAll();
        return list;
    }

    @GetMapping("selectKey")
    public List<Kinds> selectKey(String key) {
        List<Kinds> list = this.kindsService.selectKey(key);
        return list;
    }

    @GetMapping("selectByEntity")
    public List<Kinds> selectByEntity(@RequestBody HashMap<String, String> map) {
        Kinds kinds = new Kinds();
        List<Kinds> list = this.kindsService.selectByEntity(kinds);
        return list;
    }

    @GetMapping("deleteById")
    public String deleteById(Integer id) {
        this.kindsService.deleteById(id);
        return null;
    }

    @GetMapping("deleteByFlag")
    public String deleteByFlag(Integer id) {
        Kinds kinds = kindsService.queryById(id);
        kinds.setDeleteflag("1");
        kindsService.update(kinds);
        return null;
    }

    @RequestMapping("edit")
    public String edit(@RequestBody HashMap<String, String> map
    ) {
        Kinds kinds = new Kinds();
        kinds.setKid(Integer.valueOf(map.get("kid")));
        kinds.setKimg(map.get("kimg"));
        kinds.setCreatetime(TimeUtils.NowTimeN());
        kinds.setDeleteflag("0");
        kindsService.update(kinds);
        return null;
    }

    @RequestMapping("add")
    public Result add(@RequestBody HashMap<String, String> map
    ) {
        Kinds kinds = new Kinds();
        kinds.setKname(map.get("kname"));
        if(this.kindsService.queryAll(kinds).size()!=0)
        {
            kinds.setKid(this.kindsService.queryAll(kinds).get(0).getKid());
            kinds.setDeleteflag("0");
            kinds.setKimg(map.get("kimg"));
            kinds.setCreatetime(TimeUtils.NowTimeN());
            kindsService.update(kinds);
            return Result.success("添加成功");
        }
        kinds.setKimg(map.get("kimg"));
        kinds.setCreatetime(TimeUtils.NowTimeN());
        kinds.setDeleteflag("0");
        kindsService.insert(kinds);
        return Result.success("添加成功");
    }
}